<?php

namespace App\Admin\Controllers;


use App\Fault;
use App\Group;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use App\Admin\Extensions\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class FaultController extends Controller
{
    //契约 类似多重集成
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('故障管理')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('故障管理')
            ->description('详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('故障管理')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('故障管理')
            ->description('新增')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Fault);

        $grid->id('ID');
        $grid->user()->name('报修用户');
        $grid->chest()->chest_number('柜子编号');
        $grid->grip()->grip_number('格子编号');
        $grid->fault_desc('故障简述');
        $grid->state('状态')->using(['0' => '未维修', '1' => '已维修']);
        $grid->created_at('添加时间');
        $grid->updated_at('编辑时间');

        $grid->disableCreateButton();
        $grid->actions(function ($actions) {
            $actions->state('维修');
            $actions->disableView();
            $actions->disableEdit();
        });


        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Fault::findOrFail($id));

        $show->id('ID');
        $show->created_at('添加时间');
        $show->updated_at('编辑时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Fault);
        $form->display('ID');
        $form->display('添加时间');
        $form->display('编辑时间');
        return $form;
    }


    public function update($id)
    {
        if (request()->get('action') == 'state') {

            $status = false;
            $message = '修改失败';
            $model = Fault::findOrFail($id);
            $slug = $model['user_id'] . '_' . $model['chest_id'] . '_' . $model['grip_id'];
            if (!$model->isClosed()) {
                $model->update(['state' => 1]);
                $group = Group::where(['slug' => $slug,])->update(['status' => '0']);
                $status = true;
                $message = '修改成功';
            }
            return response([
                'status' => $status,
                'message' => $message,
            ]);

        } else {
            return $this->form()->update($id);
        }
    }


}
